/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.services.gl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.model.SelectItem;
import oracle.jdbc.OracleTypes;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import src.HibernateUtil;
import src.domain.gl.*;
import src.services.CRUEDService;
import src.util.Constanta;

/**
 *
 * @author Lalugina
 */
public class FstmtModel implements java.io.Serializable {
    
    private CRUEDService service;

    public FstmtModel() {
        service = new CRUEDService();
    }
    
    public List<Fstmt> findAll(Map<String, String> filters) {
        return (List<Fstmt>) service.findAll("Fstmt", filters);
    }
    
    public boolean trySave(Fstmt data) {
        if (data.getFstmtId() == null) 
        return saveData(data);
        else {
            
            removeDetail(data.getFstmtId());
            return saveData(data);
        }
    }
    
    public boolean saveData(Fstmt data) {
        
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        org.hibernate.Transaction tx = session.beginTransaction();
        boolean success = false;
        
        try {
            //sycnronizeTotals
            List<SelectItem> lineseqs = new ArrayList<>();//
            for (int i=0; i< data.getFstmtlines().size(); i++) {
                lineseqs.add(new SelectItem(data.getFstmtlines().get(i).getId().getSeq(), data.getFstmtlines().get(i).getFstmtIdSeq().toString()));
            }
   
            session.saveOrUpdate(data);
            for (int i=0; i<data.getFstmtlines().size(); i++) {
                Fstmtline fstmtline = data.getFstmtlines().get(i);
                fstmtline.setId(new FstmtlineId(data.getFstmtId(), i+1));
                fstmtline.setFstmt(data);
                session.save(fstmtline);
                
                for (int j=0; j<fstmtline.getFstmtaccs().size(); j++) {
                    Fstmtacc fstmtacc = fstmtline.getFstmtaccs().get(j);
                    fstmtacc.setId(new FstmtaccId(data.getFstmtId(), i+1, j+1));
                    fstmtacc.setFstmtline(fstmtline);
                    session.save(fstmtacc);
                }
                
                for (int j=0; j<fstmtline.getFstmttots().size(); j++) {
                    Fstmttot fstmttot = fstmtline.getFstmttots().get(j);
                    fstmttot.setId(new FstmttotId(data.getFstmtId(), i+1, j+1));
                    
                    //totals syncronizing
                    for (int x=0; x < lineseqs.size(); x++) {
                        if(fstmttot.getFstmtIdSeq() == Integer.parseInt(lineseqs.get(x).getLabel()))
                            fstmttot.setFstmtIdSeq((Integer) lineseqs.get(x).getValue());
                    }
                        
                    fstmttot.setFstmtline(fstmtline);
                    session.save(fstmttot);
                }
            }
            
            tx.commit();
            success = true;  
            
        } catch (RuntimeException e) {
            
            if (tx != null && tx.isActive()) {
                try {
                    tx.rollback();
                } catch (HibernateException e1) {
                    Logger.getLogger(FstmtModel.class.getName()).log(Level.SEVERE, null, e1);
                }
            }
            success = false;  
            Logger.getLogger(FstmtModel.class.getName()).log(Level.SEVERE, null, e);
        }
        
        finally {}
        return success;
    }
    
    public boolean removeDetail(Integer id) {
        Map<String, String> filters = new HashMap<>();
        filters.put("fstmtId", id.toString());
        Fstmt data = (Fstmt) service.getObjectSQL("from Fstmt", filters);
            
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        org.hibernate.Transaction tx = session.beginTransaction();
        boolean success = false;
        
        try {
            for (int i=0; i<data.getFstmtlines().size(); i++) {
                Fstmtline fstmtline = (Fstmtline) session.get(Fstmtline.class, data.getFstmtlines().get(i).getId());
                
                for (int j=0; j<fstmtline.getFstmtaccs().size(); j++) {
                    Fstmtacc fstmtacc = (Fstmtacc) session.get(Fstmtacc.class, fstmtline.getFstmtaccs().get(j).getId());
                    session.delete(fstmtacc);
                }
                
                for (int j=0; j<fstmtline.getFstmttots().size(); j++) {
                    Fstmttot fstmttot = (Fstmttot) session.get(Fstmttot.class, fstmtline.getFstmttots().get(j).getId());
                    session.delete(fstmttot);
                }
                session.delete(fstmtline);
            }
            
            tx.commit();
            success = true;  
            
        } catch (RuntimeException e) {
            
            if (tx != null && tx.isActive()) {
                try {
                    tx.rollback();
                } catch (HibernateException e1) {
                    Logger.getLogger(FstmtModel.class.getName()).log(Level.SEVERE, null, e1);
                }
            }
            success = false;  
            Logger.getLogger(FstmtModel.class.getName()).log(Level.SEVERE, null, e);
        }
        
        finally {}
        return success;
    }
    
    public boolean deleteData(Fstmt data) {
        return service.deleteData(data);
    }
    
    public List<FstmtReport> loadData(Integer fstmtId, Integer branchId, Integer month, Integer year) {
        
        List<FstmtReport> hasil = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        org.hibernate.Transaction tx = session.beginTransaction();
        Connection con = session.connection();  // obtain JDBC connection from Session object
        ResultSet result;
        
        CallableStatement callableStatement;
        try {
            callableStatement = con.prepareCall("{call JV_REPORT.load_finance_statement(?,?,?,?,?)}");
            callableStatement.registerOutParameter(1, OracleTypes.CURSOR);
            callableStatement.setInt(2, fstmtId);
            callableStatement.setInt(3, branchId);
            callableStatement.setInt(4, month);
            callableStatement.setInt(5, year);
            
            callableStatement.executeQuery();
     
            result = (ResultSet) callableStatement.getObject(1);
            
            while (result.next()) {
            Gledger gl = new Gledger();
            gl.setAccountName(result.getString(1));
                hasil.add(new FstmtReport(
                        result.getInt(1), result.getInt(2), result.getString(3), result.getString(4),
                        result.getBigDecimal(5), result.getBigDecimal(6), result.getBigDecimal(7), result.getString(8)));
            }

            tx.commit();
        } catch (SQLException ex) {
                
            if (tx != null && tx.isActive()) {
                try { tx.rollback();
                } catch (HibernateException e1) {}
            }
            Logger.getLogger(GledgerModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return hasil;
    }
    
    public Fstmt generateLabaRugi() {
        
        Fstmt fstmt = new Fstmt();
        fstmt.setFstmtName("LABA RUGI");
        fstmt.setFormat(Constanta.FSTMT_FORMAT_NET_CHANGE_SIMPLE);
        int lineIdx=1;
        
        //pendapatan
        String sql = 
                "from Glaccnt where glaccntId not in ("+
                "select distinct parentGlaccntId from Glaccnt where parentGlaccntId is not null) "+
                " and suspended <> 1 and accountType = "+Constanta.ACC_TYPE_PENDAPATAN+" order by accountName";
        List<Glaccnt> glPendapatan = new CRUEDService().findAllSQL(sql, null);
        
        sql = 
                "from Glaccnt where glaccntId not in ("+
                "select distinct parentGlaccntId from Glaccnt where parentGlaccntId is not null) "+
                " and suspended <> 1 and accountType = "+Constanta.ACC_TYPE_BIAYA+" order by accountName";
        List<Glaccnt> glBiaya = new CRUEDService().findAllSQL(sql, null);
        
        
        Fstmtline headLine = new Fstmtline(fstmt.getFstmtId(), lineIdx);
        headLine.setLineDescription("PENDAPATAN");
        headLine.setLineType(Constanta.FSTMT_HEADER);
        headLine.setFstmt(fstmt);
        fstmt.getFstmtlines().add(headLine);
        
        //glaccounts.size() + header + total
        int totalIdx = glPendapatan.size() + 2;
        int totrevIdx = glPendapatan.size() + glBiaya.size() + 6;
        for (Glaccnt gl : glPendapatan) {
            lineIdx++;
            Fstmtline detailLine = new Fstmtline(fstmt.getFstmtId(), lineIdx);
            
            detailLine.setLineDescription(gl.getAccountName());
            detailLine.setLineType(Constanta.FSTMT_DETAIL_REVENUE);
            detailLine.setFstmt(fstmt);
            
            //akun
            List<Fstmtacc> fsAccounts = new ArrayList<>();
            Fstmtacc fsAccount = new Fstmtacc(detailLine.getId().getFstmtId(), detailLine.getId().getSeq(), 1);
            fsAccount.setAccountNoFrom(gl.getAccountNo());
            fsAccount.setAccountNoTo(gl.getAccountNo());
            fsAccounts.add(fsAccount);
            detailLine.setFstmtaccs(fsAccounts);
            
            //total 1
            List<Fstmttot> fsTotals = new ArrayList<>();
            Fstmttot fsTotal = new Fstmttot(detailLine.getId().getFstmtId(), detailLine.getId().getSeq(), 1);
            fsTotal.setFstmtIdSeq(totalIdx);
            fsTotals.add(fsTotal);
                        
            //total 2
            Fstmttot fsTotrev = new Fstmttot(detailLine.getId().getFstmtId(), detailLine.getId().getSeq(), 2);
            fsTotrev.setFstmtIdSeq(totrevIdx);
            fsTotals.add(fsTotrev);
            detailLine.setFstmttots(fsTotals);
                                    
            fstmt.getFstmtlines().add(detailLine);
        }
        
        lineIdx++;
        Fstmtline totalLine = new Fstmtline(fstmt.getFstmtId(), totalIdx);
        totalLine.setLineDescription("Total Pendapatan");
        totalLine.setLineType(Constanta.FSTMT_TOTAL);
        fstmt.getFstmtlines().add(totalLine);
        
        lineIdx++;
        Fstmtline spaceLine = new Fstmtline(fstmt.getFstmtId(), totalIdx+1);
        spaceLine.setLineDescription("");
        spaceLine.setLineType(Constanta.FSTMT_SPARATOR);
        fstmt.getFstmtlines().add(spaceLine);
                

        //biaya
        lineIdx++;
        Fstmtline headLineb = new Fstmtline(fstmt.getFstmtId(), lineIdx);
        headLineb.setLineDescription("BIAYA");
        headLineb.setLineType(Constanta.FSTMT_HEADER);
        fstmt.getFstmtlines().add(headLineb);
        
        //glaccounts.size() + header + total + otal yg atas
        totalIdx = glBiaya.size() + 1 + lineIdx;
        for (Glaccnt gl : glBiaya) {
            lineIdx++;
            Fstmtline detailLine = new Fstmtline(fstmt.getFstmtId(), lineIdx);
            detailLine.setLineDescription(gl.getAccountName());
            detailLine.setLineType(Constanta.FSTMT_DETAIL);
            
            //akun
            List<Fstmtacc> fsAccounts = new ArrayList<>();
            Fstmtacc fsAccount = new Fstmtacc(detailLine.getId().getFstmtId(), detailLine.getId().getSeq(), 1);
            fsAccount.setAccountNoFrom(gl.getAccountNo());
            fsAccount.setAccountNoTo(gl.getAccountNo());
            fsAccounts.add(fsAccount);
            detailLine.setFstmtaccs(fsAccounts);
            
            //total
            List<Fstmttot> fsTotals = new ArrayList<>();
            Fstmttot fsTotal = new Fstmttot(detailLine.getId().getFstmtId(), detailLine.getId().getSeq(), 1);
            fsTotal.setFstmtIdSeq(totalIdx);
            fsTotals.add(fsTotal);
            
            //total 2
            Fstmttot fsTotrev = new Fstmttot(detailLine.getId().getFstmtId(), detailLine.getId().getSeq(), 2);
            fsTotrev.setFstmtIdSeq(totrevIdx);
            fsTotals.add(fsTotrev);
            detailLine.setFstmttots(fsTotals);
                                    
            fstmt.getFstmtlines().add(detailLine);
        }
        
        lineIdx++;
        Fstmtline totalLineb = new Fstmtline(fstmt.getFstmtId(), totalIdx);
        totalLineb.setLineDescription("Total Biaya");
        totalLineb.setLineType(Constanta.FSTMT_TOTAL);
        fstmt.getFstmtlines().add(totalLineb);
        
        lineIdx++;
        Fstmtline totalLinec = new Fstmtline(fstmt.getFstmtId(), totrevIdx);
        totalLinec.setLineDescription("Laba Rugi");
        totalLinec.setLineType(Constanta.FSTMT_TOTAL);
        fstmt.getFstmtlines().add(totalLinec);
        
        return fstmt;
    }
    
    public Fstmt generateNeraca() {
        
        Fstmt fstmt = new Fstmt();
        fstmt.setFstmtName("NERACA");
        fstmt.setFormat(Constanta.FSTMT_FORMAT_NET_CHANGE_SIMPLE);
        int lineIdx=1;
        
        //ACC_TYPE_HARTA
        List<Glaccnt> glHarta = new CRUEDService().findAllSQL(
            "from Glaccnt where glaccntId not in (select distinct parentGlaccntId from Glaccnt where parentGlaccntId is not null) "+
            " and suspended <> 1 and accountType = "+Constanta.ACC_TYPE_HARTA+" order by accountName", null);
        
        //ACC_TYPE_KEWAJIBAN
        List<Glaccnt> glKewajiban = new CRUEDService().findAllSQL(
            "from Glaccnt where glaccntId not in (select distinct parentGlaccntId from Glaccnt where parentGlaccntId is not null) "+
            " and suspended <> 1 and accountType = "+Constanta.ACC_TYPE_KEWAJIBAN+" order by accountName", null);
        
        //ACC_TYPE_MODAL
        List<Glaccnt> glModal = new CRUEDService().findAllSQL(
            "from Glaccnt where glaccntId not in (select distinct parentGlaccntId from Glaccnt where parentGlaccntId is not null) "+
            " and suspended <> 1 and accountType = "+Constanta.ACC_TYPE_MODAL+" order by accountName", null);
        
        //ACC_TYPE_PENDAPATAN
        List<Glaccnt> glPendapatan = new CRUEDService().findAllSQL(
            "from Glaccnt where glaccntId not in (select distinct parentGlaccntId from Glaccnt where parentGlaccntId is not null) "+
            " and suspended <> 1 and accountType = "+Constanta.ACC_TYPE_PENDAPATAN+" order by accountName", null);
        
        //ACC_TYPE_BIAYA
        List<Glaccnt> glBiaya = new CRUEDService().findAllSQL(
            "from Glaccnt where glaccntId not in (select distinct parentGlaccntId from Glaccnt where parentGlaccntId is not null) "+
            " and suspended <> 1 and accountType = "+Constanta.ACC_TYPE_BIAYA+" order by accountName", null);
        
        
        Fstmtline headLine = new Fstmtline(fstmt.getFstmtId(), lineIdx);
        headLine.setLineDescription("AKTIVA");
        headLine.setLineType(Constanta.FSTMT_HEADER);
        fstmt.getFstmtlines().add(headLine);
        
        
        int totalAsset = glHarta.size() + 2;
        for (Glaccnt gl : glHarta) {
            lineIdx++;
            Fstmtline detailLine = new Fstmtline(fstmt.getFstmtId(), lineIdx);
            detailLine.setLineDescription(gl.getAccountName());
            detailLine.setLineType(Constanta.FSTMT_DETAIL);
            detailLine.setFstmt(fstmt);
            
            //akun
            List<Fstmtacc> fsAccounts = new ArrayList<>();
            Fstmtacc fsAccount = new Fstmtacc(detailLine.getId().getFstmtId(), detailLine.getId().getSeq(), 1);
            fsAccount.setAccountNoFrom(gl.getAccountNo());
            fsAccount.setAccountNoTo(gl.getAccountNo());
            fsAccounts.add(fsAccount);
            detailLine.setFstmtaccs(fsAccounts);
            
            //total
            List<Fstmttot> fsTotals = new ArrayList<>();
            Fstmttot fsTotal = new Fstmttot(detailLine.getId().getFstmtId(), detailLine.getId().getSeq(), 1);
            fsTotal.setFstmtIdSeq(totalAsset);
            fsTotals.add(fsTotal);
            detailLine.setFstmttots(fsTotals);
                                    
            fstmt.getFstmtlines().add(detailLine);
        }
        
        lineIdx++;
        Fstmtline totalLine = new Fstmtline(fstmt.getFstmtId(), totalAsset);
        totalLine.setLineDescription("Total Aktiva");
        totalLine.setLineType(Constanta.FSTMT_TOTAL);
        fstmt.getFstmtlines().add(totalLine);
        
        lineIdx++;
        Fstmtline spaceLine = new Fstmtline(fstmt.getFstmtId(), totalAsset+1);
        spaceLine.setLineDescription("");
        spaceLine.setLineType(Constanta.FSTMT_SPARATOR);
        fstmt.getFstmtlines().add(spaceLine);
                

        //PASSIVA - ACC_TYPE_KEWAJIBAN
        lineIdx++;
        Fstmtline headLineb = new Fstmtline(fstmt.getFstmtId(), lineIdx);
        headLineb.setLineDescription("PASSIVA");
        headLineb.setLineType(Constanta.FSTMT_HEADER);
        fstmt.getFstmtlines().add(headLineb);

        lineIdx++;
        Fstmtline headLinec = new Fstmtline(fstmt.getFstmtId(), lineIdx);
        headLinec.setLineDescription("KEWAJIBAN");
        headLinec.setLineType(Constanta.FSTMT_HEADER);
        fstmt.getFstmtlines().add(headLinec);
        
        //glaccounts.size() + header + total + otal yg atas
        int totalIdx = glKewajiban.size() + lineIdx+1;
        int totalPassiva = glHarta.size() + glKewajiban.size() + glModal.size()+14;
        
        for (Glaccnt gl : glKewajiban) {
            lineIdx++;
            Fstmtline detailLine = new Fstmtline(fstmt.getFstmtId(), lineIdx);
            detailLine.setLineDescription(gl.getAccountName());
            detailLine.setLineType(Constanta.FSTMT_DETAIL);
            detailLine.setFstmt(fstmt);
            
            //akun
            List<Fstmtacc> fsAccounts = new ArrayList<>();
            Fstmtacc fsAccount = new Fstmtacc(detailLine.getId().getFstmtId(), detailLine.getId().getSeq(), 1);
            fsAccount.setAccountNoFrom(gl.getAccountNo());
            fsAccount.setAccountNoTo(gl.getAccountNo());
            fsAccounts.add(fsAccount);
            detailLine.setFstmtaccs(fsAccounts);
            
            //total
            List<Fstmttot> fsTotals = new ArrayList<>();
            Fstmttot fsTotal = new Fstmttot(detailLine.getId().getFstmtId(), detailLine.getId().getSeq(), 1);
            fsTotal.setFstmtIdSeq(totalIdx);
            fsTotals.add(fsTotal);
                        
            Fstmttot fsTotalAhir = new Fstmttot(detailLine.getId().getFstmtId(), detailLine.getId().getSeq(), 2);
            fsTotalAhir.setFstmtIdSeq(totalPassiva);
            fsTotals.add(fsTotalAhir);
            detailLine.setFstmttots(fsTotals);
            //detailLine.setFstmt(fstmt);
                                    
            fstmt.getFstmtlines().add(detailLine);
        }
        
        lineIdx++;
        Fstmtline totalLineb = new Fstmtline(fstmt.getFstmtId(), totalIdx);
        totalLineb.setLineDescription("Total Kewajiban");
        totalLineb.setLineType(Constanta.FSTMT_TOTAL);
        fstmt.getFstmtlines().add(totalLineb);
               
        
        //modal
        lineIdx++;
        Fstmtline headLined = new Fstmtline(fstmt.getFstmtId(), lineIdx);
        headLined.setLineDescription("MODAL");
        headLined.setLineType(Constanta.FSTMT_HEADER);
        fstmt.getFstmtlines().add(headLined);
        
        //glaccounts.size() + header + total + otal yg atas
        totalIdx = glModal.size() + lineIdx+1;
        for (Glaccnt gl : glModal) {
            lineIdx++;
            Fstmtline detailLine = new Fstmtline(fstmt.getFstmtId(), lineIdx);
            detailLine.setLineDescription(gl.getAccountName());
            detailLine.setLineType(Constanta.FSTMT_DETAIL);
            detailLine.setFstmt(fstmt);
            
            //akun
            List<Fstmtacc> fsAccounts = new ArrayList<>();
            Fstmtacc fsAccount = new Fstmtacc(detailLine.getId().getFstmtId(), detailLine.getId().getSeq(), 1);
            fsAccount.setAccountNoFrom(gl.getAccountNo());
            fsAccount.setAccountNoTo(gl.getAccountNo());
            fsAccounts.add(fsAccount);
            detailLine.setFstmtaccs(fsAccounts);
            
            //total
            List<Fstmttot> fsTotals = new ArrayList<>();
            Fstmttot fsTotal = new Fstmttot(detailLine.getId().getFstmtId(), detailLine.getId().getSeq(), 1);
            fsTotal.setFstmtIdSeq(totalIdx);
            fsTotals.add(fsTotal);
                                    
            Fstmttot fsTotalAhir = new Fstmttot(detailLine.getId().getFstmtId(), detailLine.getId().getSeq(), 2);
            fsTotalAhir.setFstmtIdSeq(totalPassiva);
            fsTotals.add(fsTotalAhir);
            detailLine.setFstmttots(fsTotals);
            //detailLine.setFstmt(fstmt);
            
            fstmt.getFstmtlines().add(detailLine);
        }
        
        lineIdx++;
        Fstmtline totalLinec = new Fstmtline(fstmt.getFstmtId(), totalIdx);
        totalLinec.setLineDescription("Total Modal");
        totalLinec.setLineType(Constanta.FSTMT_TOTAL);
        fstmt.getFstmtlines().add(totalLinec);
        
        lineIdx++;
        Fstmtline spaceLine2 = new Fstmtline(fstmt.getFstmtId(), lineIdx);
        spaceLine2.setLineDescription("");
        spaceLine2.setLineType(Constanta.FSTMT_SPARATOR);
        fstmt.getFstmtlines().add(spaceLine2);
        
        //LABA_RUGI BERJALAN
        //pendapatan
        lineIdx++;
        Fstmtline headLiner = new Fstmtline(fstmt.getFstmtId(), lineIdx);
        headLiner.setLineDescription("LABA RUGI BERJALAN");
        headLiner.setLineType(Constanta.FSTMT_HEADER);
        fstmt.getFstmtlines().add(headLiner);
        
        lineIdx++;
        Fstmtline revline = new Fstmtline(fstmt.getFstmtId(), lineIdx);
        revline.setLineDescription("Pendapatan");
        revline.setLineType(Constanta.FSTMT_DETAIL_REVENUE);
        revline.setFstmt(fstmt);
        List<Fstmtacc> fsAccounts = new ArrayList<>();
        int n=1;
        for (Glaccnt gl : glPendapatan) {
            Fstmtacc fsAccount = new Fstmtacc(revline.getId().getFstmtId(), revline.getId().getSeq(), n);
            fsAccount.setAccountNoFrom(gl.getAccountNo());
            fsAccount.setAccountNoTo(gl.getAccountNo());
            fsAccounts.add(fsAccount);
            n++;
        }
        revline.setFstmtaccs(fsAccounts);
        
        List<Fstmttot> fsTotals = new ArrayList<>();
        Fstmttot fsTotal = new Fstmttot(revline.getId().getFstmtId(), revline.getId().getSeq(), 1);
        fsTotal.setFstmtIdSeq(lineIdx+2);
        fsTotals.add(fsTotal);
                                    
        Fstmttot fsTotalAhir = new Fstmttot(revline.getId().getFstmtId(), revline.getId().getSeq(), 2);
        fsTotalAhir.setFstmtIdSeq(totalPassiva);
        fsTotals.add(fsTotalAhir);
        revline.setFstmttots(fsTotals);
        fstmt.getFstmtlines().add(revline);
        
       
        //biaya    
        lineIdx++;
        Fstmtline expline = new Fstmtline(fstmt.getFstmtId(), lineIdx);
        expline.setLineDescription("Biaya");
        expline.setLineType(Constanta.FSTMT_DETAIL);
        expline.setFstmt(fstmt);
        n=1;    
        List<Fstmtacc> fsAccounts2 = new ArrayList<>();
        for (Glaccnt gl : glBiaya) {
            Fstmtacc fsAccount = new Fstmtacc(expline.getId().getFstmtId(), expline.getId().getSeq(), n);
            fsAccount.setAccountNoFrom(gl.getAccountNo());
            fsAccount.setAccountNoTo(gl.getAccountNo());
            fsAccounts2.add(fsAccount);
            n++;
        }
        expline.setFstmtaccs(fsAccounts2);
        //total
        List<Fstmttot> fsTotalsb = new ArrayList<>();
        Fstmttot fsTotalb = new Fstmttot(expline.getId().getFstmtId(), expline.getId().getSeq(), 1);
        fsTotalb.setFstmtIdSeq(lineIdx+1);
        fsTotalsb.add(fsTotalb);
                                    
        Fstmttot fsTotalAhirb = new Fstmttot(expline.getId().getFstmtId(), expline.getId().getSeq(), 2);
        fsTotalAhirb.setFstmtIdSeq(totalPassiva);
        fsTotalsb.add(fsTotalAhirb);
        expline.setFstmttots(fsTotalsb);
        fstmt.getFstmtlines().add(expline);
        
        lineIdx++;
        Fstmtline headLinertb = new Fstmtline(fstmt.getFstmtId(), lineIdx);
        headLinertb.setLineDescription("Total Laba Rugi Berjalan");
        headLinertb.setLineType(Constanta.FSTMT_TOTAL);
        fstmt.getFstmtlines().add(headLinertb);
        
        lineIdx++;
        Fstmtline pasivaLine = new Fstmtline(fstmt.getFstmtId(), totalPassiva);
        pasivaLine.setLineDescription("Total PASSIVA");
        pasivaLine.setLineType(Constanta.FSTMT_TOTAL);
        fstmt.getFstmtlines().add(pasivaLine);
        
        return fstmt;
    }
}
